<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.keyword" placeholder="关键字"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="search">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>



        <!--列表-->
        <el-table :data="DictionaryType" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="sn" label="编号" width="120" sortable>
            </el-table-column>
            <el-table-column prop="name" label="数据字典名" width="160" sortable>
            </el-table-column>
            <el-table-column label="操作">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next"
                           @current-change="handleCurrentChange"
                           :page-size="pageSize"
                           :total="total"
                           :current-page="currentPage"
                           style="float:right;">
            </el-pagination>
        </el-col>

        <!--新增或编辑界面-->
        <!--
           :model  绑定模型层的表单数据
           :visible.sync  控制表单是否显示
           :rules  绑定表单校验规则
           ref="DictionaryTypeForm"  给当前表单去一个引用名
-->
        <el-dialog :title="title" :visible.sync="DictionaryTypeFormVisible" :close-on-click-modal="false">
            <el-form :model="DictionaryTypeForm" label-width="80px" :rules="DictionaryTypeFormRules" ref="DictionaryTypeForm">
                <el-form-item label="编号" prop="sn">
                    <el-input v-model="DictionaryTypeForm.sn" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="名称" prop="name">
                    <el-input v-model="DictionaryTypeForm.name" auto-complete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="DictionaryTypeFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="DictionaryTypeSubmit" :loading="DictionaryTypeLoading">提交</el-button>
            </div>
        </el-dialog>

    </section>
</template>

<script>
    export default {
        data() {
            return {
                title: "",
                filters: {
                    keyword: ''//关键字
                },
                DictionaryType: [],
                employees: [],
                deptTree: [],
                total: 0,
                currentPage: 1,
                pageSize: 5,
                listLoading: false,
                sels: [],//列表选中行

                DictionaryTypeFormVisible: false,//编辑界面是否显示
                DictionaryTypeLoading: false,
                DictionaryTypeFormRules: {//表单校验规则
                    name: [
                        { required: true, message: '请输入姓名', trigger: 'blur' }
                    ]
                },
                //编辑界面数据
                DictionaryTypeForm: {
                    id: null,
                    name: "",
                    sn: "",
                    state: 0,

                },
            }
        },
        methods: {
            //高级查询要定位到第一页
            search(){
                this.currentPage = 1;
                this.getSystemDictionaryType();
            },

            handleCurrentChange(val) {
                this.currentPage = val;
                this.getSystemDictionaryType();
            },
            //获取员工列表
            getSystemDictionaryType() {
                let para = {
                    currentPage: this.currentPage,
                    pageSize:this.pageSize,
                    keyword: this.filters.keyword//关键字
                };
                this.listLoading = true;//开启忙等框
                this.$http.post("/dict/list",para)
                    .then(result=>{
                        this.listLoading = false;//关闭忙等框
                        result = result.data;
                        this.DictionaryType = result.rows;
                        this.total = result.total;//总条数
                    })
                    .catch(result=>{
                        this.$message({
                            message: '系统繁忙，稍后重试！',
                            type: 'error'
                        });
                    })
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '温馨提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;//开启列表加载忙等
                    this.$http.delete("/dict/"+row.id)
                        .then(result=>{//后端执行完成了
                            this.listLoading = false;//关闭列表加载忙等
                            result = result.data;//  AjaxResult
                            //{success:true,message:操作成功}
                            //{success:false,message:操作失败}
                            //1.提示
                            if(result.success){
                                this.$message({
                                    message: '操作成功！',
                                    type: 'success'
                                });
                            }else{
                                this.$message({
                                    message: result.message,//提示后端的业务错误
                                    type: 'error'
                                });
                            }
                            //2.刷新页面  是否需要定位到第一
                            this.currentPage = 1;
                            this.getSystemDictionaryType();
                        })
                        .catch(result=>{
                            this.listLoading = false;//关闭列表加载忙等
                            console.log(result);
                            //1.提示
                            this.$message({
                                message: '系统繁忙，稍后重试！！！',
                                type: 'error'
                            });
                            //2.刷新页面 要   是否需要定位到第一页
                            this.currentPage = 1;
                            this.getSystemDictionaryType();
                        });
                }).catch(() => {

                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.title = "编辑";
                this.DictionaryTypeFormVisible = true;//控制编辑模态框显示
                //Object.assign({}, row); 将当前行数据克隆一份
                this.DictionaryTypeForm = Object.assign({}, row);//行数据回显
                let dirPath = row.dirPath;
                if(dirPath){
                    dirPath = dirPath.split("/");
                    let parent = [];
                    for(let i=1; i<dirPath.length-1; i++){
                        parent.push(parseInt(dirPath[i]));
                    }
                    console.log(parent);
                    this.DictionaryTypeForm.parent = parent;
                }


            },
            //显示新增界面
            handleAdd: function () {

                this.title = "新增";
                this.DictionaryTypeFormVisible = true;//控制新增模态框显示
                this.DictionaryTypeForm = {
                    id: null,
                    name: "",
                    sn: "",
                    state: 0,
                };
            },
            //编辑
            DictionaryTypeSubmit: function () {
                //从当前页面中申明的所用引用，从引用中找到DictionaryTypeForm的引用，直接调用表单校验validate
                this.$refs.DictionaryTypeForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.departmentLoading = true;//开启按钮忙等框
                            let para = Object.assign({}, this.DictionaryTypeForm);
                            //处理后端需要的格式
                            let parent = para.parent;
                            if(parent){
                                para.parent = {"id":parent[parent.length-1]}
                            }
                            this.$http.put("/dict",para)
                                .then(result=>{//后端执行完成了
                                    this.departmentLoading = false;//关闭按钮忙等框
                                    result = result.data;//  AjaxResult
                                    //1.提示
                                    if(result.success){
                                        this.$message({
                                            message: '操作成功！',
                                            type: 'success'
                                        });
                                    }else{
                                        this.$message({
                                            message: result.message,//提示后端的业务错误
                                            type: 'error'
                                        });
                                    }
                                    //2.刷新页面  是否需要定位到第一
                                    this.currentPage = 1;
                                    this.DictionaryTypeFormVisible = false;
                                    this.getSystemDictionaryType();
                                })
                                .catch(result=>{
                                    this.departmentLoading = false;//关闭按钮忙等框
                                    console.log(result);
                                    //1.提示
                                    this.$message({
                                        message: '系统繁忙，稍后重试！！！',
                                        type: 'error'
                                    });
                                });
                        });
                    }
                });
            },
            selsChange: function (sels) {//sels当前列表选中的所有行数据
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;//开启列表忙等框
                    this.$http.patch("/dict",ids)
                        .then(result=>{//后端执行完成了
                            this.listLoading = false;//关闭列表加载忙等
                            result = result.data;//  AjaxResult
                            //{success:true,message:操作成功}
                            //{success:false,message:操作失败}
                            //1.提示
                            if(result.success){
                                this.$message({
                                    message: '操作成功！',
                                    type: 'success'
                                });
                            }else{
                                this.$message({
                                    message: result.message,//提示后端的业务错误
                                    type: 'error'
                                });
                            }
                            //2.刷新页面  是否需要定位到第一
                            this.currentPage = 1;
                            this.getSystemDictionaryType();
                        })
                        .catch(result=>{
                            this.listLoading = false;//关闭列表加载忙等
                            console.log(result);
                            //1.提示
                            this.$message({
                                message: '系统繁忙，稍后重试！！！',
                                type: 'error'
                            });
                            //2.刷新页面 要   是否需要定位到第一页
                            this.currentPage = 1;
                            this.getSystemDictionaryType();
                        });
                }).catch(() => {
                });
            },
        },
        mounted() {
            this.getSystemDictionaryType();
        }
    }

</script>

<style scoped>

</style>